package reflect;

import sun.applet.Main;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author Zhouchb
 * @Create 2021-10-2021/10/24 :14:48
 * @Description
 */
public class Reflect1024 {
    public static void main(String[] args) throws Exception {
        Class clazz = Persion.class;

        Constructor constructor = clazz.getConstructor(String.class);
        Object obj = constructor.newInstance("林动");
        Persion persion = (Persion)obj;
        System.out.println(persion);
        //获取属性
        Field name = clazz.getDeclaredField("name");
        name.set(persion,"今朝");
        System.out.println(persion);
        //方法
        Method show = clazz.getDeclaredMethod("show");
        show.invoke(persion);

        //******************调用私有的方法，属性，构造函数
        Class clazz1 = Persion.class;
        Constructor con = clazz1.getDeclaredConstructor(Integer.class);
        con.setAccessible(true);
        Persion o = (Persion) con.newInstance(24);
        //私有属性
        Field age = clazz1.getDeclaredField("age");
        age.setAccessible(true);
        age.set(o,22);
        //私有方法
        Method showInfo = clazz1.getDeclaredMethod("showInfo", String.class);
        showInfo.setAccessible(true);
        String info = (String) showInfo.invoke(o,"萧炎");
        System.out.println(info);
        System.out.println(o);
    }
}

